/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PCFGRuleManager;

import Constant.DataPath;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

/**
 *
 * @author tuyentran
 */
public class PCFGParsingRuleProcessor {

    private HashMap<String, ArrayList<PCFGParsingRule>> ruleMap = new HashMap<String, ArrayList<PCFGParsingRule>>();
    private ArrayList<PCFGParsingRule> pprSet = new ArrayList<PCFGParsingRule>();
    private ArrayList<String> parsingLabels = new ArrayList<String>();

    public PCFGParsingRuleProcessor() throws IOException {
        extractPOS();
        readPCFGRuleFromFile();
        //createPOSFile();
    }

    /**
     * @function add s into parsingLabels
     *
     * @param s
     */
    private void addLabel(String s) {
        if (!parsingLabels.contains(s)) {
            parsingLabels.add(s);
        }
    }

    /**
     * @function add ss into parsingLabels
     *
     * @param ss
     */
    private void addLabels(String... ss) {
        for (int i = 0; i < ss.length; i++) {
            String string = ss[i];
            if (!parsingLabels.contains(string)) {
                parsingLabels.add(string);
            }
        }
    }

    /**
     * @function extract POS(part-of-speech or parsing label) from PCFG Parsing
     * Rule
     *
     * @throws FileNotFoundException
     */
    private void extractPOS() throws IOException {
        BufferedReader bfr1 = new BufferedReader(new InputStreamReader(new FileInputStream(DataPath.normalPCFGRuleFilePath3), "utf-8"));
        String str1 = "";
        while ((str1 = bfr1.readLine()) != null) {
            String[] comp = str1.split("   ");

            // <editor-fold desc="extract the parsing label">
            addLabel(comp[0]);
            addLabels(comp[1].split(" "));
            // </editor-fold>
        }
    }

    /**
     * @function write the POS list into the file
     * @throws FileNotFoundException 
     */
    private void createPOSFile() throws FileNotFoundException {
        PrintWriter pw = new PrintWriter(DataPath.partOfSpeechFilePath3);
        Collections.sort(parsingLabels);
        for (int i = 0; i < parsingLabels.size(); i++) {
            String string = parsingLabels.get(i);
            pw.println(string);
        }
        pw.close();
    }

    private void putToRuleMap(PCFGParsingRule rule) {
        if (ruleMap.containsKey(rule.getRightSide())){
            ruleMap.get(rule.getRightSide()).add(rule);
        } else {
            ArrayList<PCFGParsingRule> arr = new ArrayList<PCFGParsingRule>();
            arr.add(rule);
            ruleMap.put(rule.getRightSide(), arr);
        }
    }

    
    /**
     * @function read the set of PCFG parsing rule from file
     *
     * @throws UnsupportedEncodingException
     * @throws IOException
     */
    private void readPCFGRuleFromFile() throws UnsupportedEncodingException, IOException {
        BufferedReader bfr1 = new BufferedReader(new InputStreamReader(new FileInputStream(DataPath.normalPCFGRuleFilePath3), "utf-8"));
        String str1 = "";
        while ((str1 = bfr1.readLine()) != null) {
            String[] comp = str1.split("   ");

            // <editor-fold desc="process for PCFG rule">
            PCFGParsingRule p = new PCFGParsingRule(comp[0], comp[1]);
            int i1 = Integer.parseInt(comp[2]);
            int i2 = Integer.parseInt(comp[3]);
            int i3 = Integer.parseInt(comp[4]);
            double pro1 = Math.log10((double) i1 / i2);
            double pro2 = Math.log10((double) i1 / i3);
            p.setProbability(pro1, pro2);
            pprSet.add(p);
            putToRuleMap(p);
            // </editor-fold>
        }
    }

    /**
     * 
     * @param s
     * @return 
     */
    public ArrayList<PCFGParsingRule> getByRightSide(String s) {
        return ruleMap.get(s);
    }

    /**
     * @return the pprSet
     */
    public ArrayList<PCFGParsingRule> getPprSet() {
        return pprSet;
    }

    /**
     * @return the parsingLabel
     */
    public ArrayList<String> getParsingLabels() {
        return parsingLabels;
    }

    /**
     * @return the ruleMap
     */
    public HashMap<String, ArrayList<PCFGParsingRule>> getRuleMap() {
        return ruleMap;
    }
    
    public static void main(String[] args) throws IOException {
        PCFGParsingRuleProcessor pprp = new PCFGParsingRuleProcessor();
    }
}
